package net.httpconn;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;

public class PutCommand extends HttpCommand {
	

	public String execute() {
		
		StringBuffer responseBuffer = new StringBuffer();
		BufferedReader bReader = null;
		String response = null;
		
		try {
			OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
			out.write(content);
			out.write("\r\n");
			out.flush();
			
			if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				
				bReader =  new BufferedReader(new InputStreamReader(connection.getInputStream()));				
				String line = null;

				while ((line = bReader.readLine()) != null) {
					responseBuffer.append(line);
				}
			} 
			else
				responseBuffer.append(connection.getResponseCode());
			
			response = responseBuffer.toString();
			
		} 
		catch (Throwable t) {
			response = ExceptionLogger.logThrowable(t);
		}
		finally	{super.finalizeConnection();}
		
		return response;
	}
	
}
